package com.llmoe.print.controller;

import com.google.zxing.WriterException;
import com.llmoe.print.common.PrintUtil;
import com.llmoe.print.common.ZxingUtils;
import com.llmoe.print.common.pdf.PdfText2Url;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPrintable;
import org.apache.pdfbox.printing.Scaling;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@RestController
@CrossOrigin
public class testController {

    /**
     * pdf
     */
    @PostMapping("/url")
    public Boolean pdf(@RequestBody String url) throws Exception {
        //直接打印
//        url = java.net.URLDecoder.decode(url, "UTF-8");
        PdfText2Url.readPdf(url);
        return true;
    }

    /**
     * pdf
     */
    @GetMapping("/urls")
    public Boolean pdfs(String url) throws Exception {
        System.out.println(url);
        return true;

    }

    /**
     * 打印
     */
    @GetMapping("/print")
    public Boolean print(String contcent, String text) throws InterruptedException {
        //二维码宽度
        int width = 80;
        //二维码高度
        int height = 80;
        //二维码内容
        BufferedImage zxingImage = null;
        try {
            //二维码图片流
            zxingImage = ZxingUtils.enQRCode(contcent, width, height);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        //背景图片地址
        String backgroundPath = "D:\\out\\imageSort\\1.jpg";
        InputStream inputStream = null;
        try {
            //合成二维码和背景图
            BufferedImage image = ZxingUtils.drawImage(backgroundPath, zxingImage, 0, 0);
            //绘制文字
            //读取本地字体
            //Font font = new Font("default", Font.BOLD, 35);
            //读取自定义物理字体
            Font font = new Font("微软雅黑",Font.PLAIN,12);
            image = ZxingUtils.drawString(image, text, 0, 90,font,Color.black);
            //图片转inputStream
            inputStream = ZxingUtils.bufferedImageToInputStream(image);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //保存的图片路径
        Date dNow = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy\\MM\\dd\\");
        String originalFileName = "D:\\out\\imageSort\\"+ft.format(dNow)+System.currentTimeMillis()+".png";
        PdfText2Url.isexitsPath(originalFileName);
        try {
            //保存为本地图片
            ZxingUtils.saveFile(inputStream, originalFileName);
            new PrintUtil().drawImage(originalFileName, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

}
